package edu.ccut.saturn.studio.wfg.dialog.routerdialog;

import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.wfg.dialog.SelectionCandidate;
import edu.ccut.saturn.studio.wfg.model.Connection;
import edu.ccut.saturn.studio.wfg.model.Shape;
import edu.ccut.saturn.studio.wfg.wfgmodel.SaturnRouter;
import edu.ccut.saturn.studio.wfg.wfgmodel.SaturnRouterNode;

public class SaturnRouterConnectionDialog extends TitleAreaDialog {
	
	private Text nameText, keyText;
	
	private Label nameLabel, keyLabel;
	
	private Connection connectionShape;
	
	private SaturnRouterNode source;
	
	private Shape target;
	
	private SaturnRouter saturnRouter;
	
	public SaturnRouterConnectionDialog(Shell parentShell , Connection connectionShape) {
		super(parentShell);
		this.connectionShape = connectionShape;
	}
	
	protected Control createDialogArea(Composite parent) {
		
		setTitleImage(Activator.getImage(Images.SATURN_ICONS_MAXNAME));
		setMessage(SelectionCandidate.SETTING_ATTRIBUTE);
		
		Composite area = (Composite) super.createDialogArea(parent);
		final Composite composite = new Composite(area, SWT.NONE);
		
		final GridData gd_composite = new GridData(SWT.FILL, SWT.FILL, true, true);
		gd_composite.heightHint = 158;
		gd_composite.widthHint = 293;
		composite.setLayoutData(gd_composite);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		composite.setLayout(gridLayout);
		
		nameLabel = new Label(composite, SWT.NONE);
		final GridData gd_nameLabel = new GridData();
		gd_nameLabel.verticalIndent = 10;
		nameLabel.setLayoutData(gd_nameLabel);
		nameLabel.setText(Messages.getString("SaturnRouterConnectionDialog.Name"));   //$NON-NLS-1$
		
		nameText = new Text(composite, SWT.BORDER);
		final GridData gd_nameText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_nameText.verticalIndent = 10;
		nameText.setLayoutData(gd_nameText);
		
		keyLabel = new Label(composite, SWT.NONE);
		final GridData gd_label = new GridData();
		gd_label.verticalIndent = 10;
		keyLabel.setLayoutData(gd_label);
		keyLabel.setText(Messages.getString("SaturnRouterConnectionDialog.Express"));   //$NON-NLS-1$

		keyText = new Text(composite, SWT.BORDER);
		final GridData gd_textKey = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_textKey.verticalIndent = 10;
		keyText.setLayoutData(gd_textKey);
		
		nameText.setText(this.saturnRouter.getLineName());
		keyText.setText(this.saturnRouter.getKey());
		
		return area;
	}
	
	protected Point getInitialSize() {
		return new Point(400, 220);
	}
	
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setImage(Activator.getImage(Images.SATURN_ICONS_SMALLNAME));
		newShell.setText(SelectionCandidate.ATTRIBUTE_SETTING);
	}
	
	protected void buttonPressed(int buttonId) {
		this.saturnRouter.setLineName(nameText.getText().trim());
		this.saturnRouter.setKey(keyText.getText().trim());
		connectionShape.setLineName(nameText.getText().trim());
		super.buttonPressed(buttonId);
	}
	
	private void getRouter(){
		for(SaturnRouter router : this.source.getRouterList()){
			if(this.target.getId().equals(router.getId())){
				this.saturnRouter = router;
			}
		}
	}

	/**
	 * 只有源是SaturnRouterNode才能弹出该对话框
	 * 
	 * */
	@Override
	public int open() {
		if(!(connectionShape.getSource() instanceof SaturnRouterNode)){
			return -1;
		}
		this.source = (SaturnRouterNode)connectionShape.getSource();
		this.target = connectionShape.getTarget();
		getRouter();
		return super.open();
	}
}